package com.sxu.generate;

import java.text.DecimalFormat;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxu.data.CompanyLocation;
import com.sxu.data.SiteLocation;
import com.sxu.entity.Location;
import com.sxu.fileprocess.ReadCSV;

import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;

public class GenerateJsonArray {
	public JSONArray generateJSONArray(String path, String sitename, String pollutant, String industry)
			throws Exception {
		JSONArray ja = new JSONArray();

		String[][] xlsArr = ReadCSV.getCsvData(path);
		for (int i = 1; i < xlsArr.length; i++) {
			for (int j = 1; j < xlsArr[0].length; j++) {
				Float value = Float.parseFloat(xlsArr[i][j]);
				int time = Integer.parseInt(xlsArr[i][0]);
				int company = Integer.parseInt(xlsArr[0][j]);
				Location s = CompanyLocation.getCompanyMap("file/company.csv").get(company);
				Float[] coordinates = { s.getLon(), s.getLat() };
				JSONObject jo = GenerateJson.generateEachJSON(sitename, pollutant, industry, time, company, value,
						coordinates);
				// System.out.println(jo);
				ja.add(jo);
			}
		}
		return ja;
	}
//查看经纬度数据
/*	public static void main(String[] args) throws Exception {
		NetcdfFile ncFile = NetcdfFile.open("C:\\Users\\FELIX\\Desktop\\CZ_1KM_2019011312.nc");
		Variable lonV = ncFile.findVariable("lon");
		float[] lonData = (float[]) lonV.read().copyTo1DJavaArray();
		for (int i = 0; i < lonData.length; i++) {
			System.out.print(lonData[i] + ",");
		}
		System.out.println();
		Variable latV = ncFile.findVariable("lat");
		float[] latData = (float[]) latV.read().copyTo1DJavaArray();
		for (int i = 0; i < latData.length; i++) {
			System.out.print(latData[i] + ",");
		}
		System.out.println();
		System.out.println(lonData.length);
		System.out.println(latData.length);
	}*/
//查看污染物p在t时刻的数据
	/*public static void main(String[] args) throws Exception {
		NetcdfFile ncFile = NetcdfFile.open("C:\\Users\\FELIX\\Desktop\\CZ_1KM_2019011312.nc");
		Variable data = ncFile.findVariable("results");
		float[][][][] dataArr = (float[][][][]) data.read().copyToNDJavaArray();
		// 污染物编号
		int p = 6;
		// 时间
		int t = 23;
		System.out.println(dataArr[p][t].length);
		System.out.println(dataArr[p][t][0].length);
		DecimalFormat df2 = new DecimalFormat("###.00");
		for (int i = 0; i < dataArr[p][t].length; i++) {
			for (int j = 0; j < dataArr[p][t][0].length; j++) {
				System.out.print(df2.format(dataArr[p][t][i][j]) + ",");
			}
		}
		System.out.println();
	}*/

	public JSONArray generateAirQualityJSONArray(String path, int m) throws Exception {
		JSONArray aqja = new JSONArray();

		NetcdfFile ncFile = NetcdfFile.open(path);
		Variable lonV = ncFile.findVariable("lon");
		float[] lonData = (float[]) lonV.read().copyTo1DJavaArray();
		Variable latV = ncFile.findVariable("lat");
		float[] latData = (float[]) latV.read().copyTo1DJavaArray();
		Variable data = ncFile.findVariable("results");
		float[][][][] dataArr = (float[][][][]) data.read().copyToNDJavaArray();
		JSONObject jo = null;
		// for (int m = 0; m < dataArr.length; m++) {
		// 污染物
		String pollutant;
		switch (m) {
		case 0:
			pollutant = "PM2.5";
			break;
		case 1:
			pollutant = "PM10";
			break;
		case 2:
			pollutant = "SO2";
			break;
		case 3:
			pollutant = "NO2";
			break;
		case 4:
			pollutant = "CO";
			break;
		case 5:
			pollutant = "O3";
			break;
		case 6:
			pollutant = "AQI";
			break;
		default:
			throw new RuntimeException();
		}
		for (int n = 0; n < dataArr[0].length; n++) {
			int time = n;
			for (int p = 0; p < dataArr[0][0].length; p++) {
				float lat = latData[p];
				for (int q = 0; q < dataArr[0][0][0].length; q++) {
					float lon = lonData[q];
					float value = dataArr[m][n][p][q];
					Float[] coordinates = { lon, lat };
					jo = GenerateJson.generateEachAirQualityJSON(pollutant, time, value, coordinates);
					System.out.println(jo);
					aqja.add(jo);
				}
			}
		}
		// }
		return aqja;
	}

	public JSONArray generateChangzhiForcastJSONArray(String path) throws Exception {
		JSONArray ja = new JSONArray();
		JSONObject jo = null;
		String[][] xlsArr = ReadCSV.getCsvData(path);
		System.out.println(xlsArr.length);
		for (int i = 1; i < xlsArr.length; i++) {
			String time = xlsArr[i][0];
			int temp = Integer.parseInt(xlsArr[i][1]);
			int pressure = Integer.parseInt(xlsArr[i][2]);
			int rh = Integer.parseInt(xlsArr[i][3]);
			int windSpeed = Integer.parseInt(xlsArr[i][4]);
			String windDirect = xlsArr[i][5];
			int totalCloudCover = Integer.parseInt(xlsArr[i][6]);
			jo = GenerateJson.generateEachChangzhiForecastJSON(time, temp, pressure, rh, windSpeed, windDirect,
					totalCloudCover);
			System.out.println(jo);
			ja.add(jo);
		}
		return ja;
	}

	public JSONArray generateSiteConditionJSONArray(String path, String sitename) throws Exception {
		JSONArray ja = new JSONArray();
		JSONObject jo = null;
		String[][] xlsArr = ReadCSV.getCsvData(path);
		System.out.println(xlsArr.length);
		for (int i = 1; i < xlsArr.length; i++) {
			Integer time = Integer.parseInt(xlsArr[i][0]);
			Float pm2_5 = Float.parseFloat(xlsArr[i][1]);
			Float pm10 = Float.parseFloat(xlsArr[i][2]);
			Float so2 = Float.parseFloat(xlsArr[i][3]);
			Float no2 = Float.parseFloat(xlsArr[i][4]);
			Float co = Float.parseFloat(xlsArr[i][5]);
			Float o3 = Float.parseFloat(xlsArr[i][6]);
			Float aqi = Float.parseFloat(xlsArr[i][7]);
			Location ss = SiteLocation.siteMap.get(sitename);
			Float[] coordinates = { ss.getLon(), ss.getLat() };
			jo = GenerateJson.generateSiteConditionJSON(sitename, pm2_5, pm10, so2, no2, co, o3, aqi, time,
					coordinates);
			System.out.println(jo);
			ja.add(jo);
		}
		return ja;
	}
}
